package com.macasa.panel.entity.impl;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Version;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import com.google.common.base.Objects;
import com.macasa.enumerations.NetworkArch;
import com.macasa.enumerations.XenArch;
import com.macasa.enumerations.XenNetworkType;
import com.macasa.panel.common.utils.CastHelpers;
import com.macasa.panel.entity.api.IInterface;
import com.macasa.panel.entity.api.IXenServer;

@Entity()
@Table(name = "xen_server")
@Getter
@Setter
@ToString(callSuper = true)
public class XenServer extends Server implements IXenServer {

	/**
	 * The Constant serialVersionUID
	 */
	@Version
	private static final long	serialVersionUID	= 5438463205046020901L;

	@Enumerated(EnumType.STRING)
	@Column(name = "network_arch", nullable = false)
	private NetworkArch			networkArch;

	@Column(name = "lvm_group_name", nullable = false)
	private String				lvmGroupName;

	@Column(name = "lvm_group_size", nullable = false)
	private Integer				lvmGroupSize;

	@Column(name = "memory_size", nullable = false)
	private Integer				memorySize;

	@Column(name = "dom_memory_size", nullable = false)
	private Integer				domMemorySize;

	@Enumerated(EnumType.STRING)
	@Column(name = "xen_arch", nullable = false)
	private XenArch				xenArch;

	@Column(name = "xen_network_type", nullable = false)
	private XenNetworkType		xenNetworkType;

	@Column(name = "xen_deployed", nullable = false)
	private Boolean				xenDeployed;

	@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = com.macasa.panel.entity.impl.Interface.class)
	@JoinTable(name = "xen_server_interfaces", joinColumns = @JoinColumn(name = "fk_server_id"), inverseJoinColumns = @JoinColumn(name = "fk_interface_id"))
	private List<IInterface>	interfaces;

	@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = com.macasa.panel.entity.impl.Interface.class)
	@JoinTable(name = "xen_server_failovers", joinColumns = @JoinColumn(name = "fk_server_id"), inverseJoinColumns = @JoinColumn(name = "fk_interface_id"))
	private List<IInterface>	failovers;

	@Override
	public boolean equals(final Object obj) {

		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof XenServer)) {
			return false;
		}

		final XenServer other = CastHelpers.cast(obj, XenServer.class);

		return super.equals(obj) && Objects.equal(this.networkArch, other.networkArch) && Objects.equal(this.lvmGroupName, other.lvmGroupName)
				&& Objects.equal(this.lvmGroupSize, other.lvmGroupSize) && Objects.equal(this.memorySize, other.memorySize)
				&& Objects.equal(this.domMemorySize, other.domMemorySize) && Objects.equal(this.xenArch, other.xenArch)
				&& Objects.equal(this.xenNetworkType, other.xenNetworkType) && Objects.equal(this.xenDeployed, other.xenDeployed)
				&& Objects.equal(this.interfaces, other.interfaces) && Objects.equal(this.failovers, other.failovers);
	}

	@Override
	public int hashCode() {
		return super.hashCode()
				+ Objects.hashCode(this.networkArch, this.lvmGroupName, this.lvmGroupSize, this.memorySize, this.domMemorySize, this.xenArch,
						this.xenNetworkType, this.xenDeployed, this.interfaces, this.failovers);
	}
}
